﻿using LanguagePolish.Base;
using LanguagePolish.EntityModel;
using LanguagePolish.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using System.Windows;
using System.Windows.Input;
using LanguagePolish.Views;

namespace LanguagePolish.ViewModels
{
    class StudentsViewModel : BaseViewModel
    {
        private StudentsModel model;

        private DispatcherTimer timer;

        private bool isArchieved = false;
        public bool IsArchieved 
        {
            get { return isArchieved; }
            set 
            {
                isArchieved = value;
                UpdateStudentCollection();
                OnPropertyChanged("IsArchieved");
            }
        }

        private string searchPattern;
        public string SearchPattern
        {
            get { return searchPattern; }
            set
            {
                searchPattern = value;
                UpdateStudentsCollectionWithWaiting();
                OnPropertyChanged("SearchPattern");
            }
        }


        private ObservableCollection<Student> students;

        public ObservableCollection<Student> Students { get { return students; } }

        public ICommand AddStudentCommand { get; private set; }
        public ICommand EditStudentCommand { get; private set; }
        public ICommand RemoveStudentCommand { get; private set; }
        public ICommand ShowInfoCommand { get; private set; }

        public StudentsViewModel()
        {
            model = new StudentsModel();
            AddStudentCommand = new RelayCommand(AddNewStudent, (o) => model.CanUserAddStudents);
            EditStudentCommand = new RelayCommand((o) => EditStudent((Student)o), (o) => model.CanUserEditStudent);
            RemoveStudentCommand = new RelayCommand((o) => RemoveStudent((Student)o), (o) => model.CanUserRemoveStudents);
            ShowInfoCommand = new RelayCommand((o) => ShowStudentInfo((Student)o));

            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 300);
            timer.Tick += timer_RefreshFilter;

            UpdateStudentCollection();
        }

        private void UpdateStudentsCollectionWithWaiting()
        {
            timer.Stop();
            timer.Start();
        }

        private void UpdateStudentCollection()
        {
            var res = model.GetStudentsWithFilter(searchPattern, isArchieved);
            students = new ObservableCollection<Student>(res);
            OnPropertyChanged("Students");
            timer.Stop();
        }

        void timer_RefreshFilter(object sender, EventArgs e)
        {
            UpdateStudentCollection();
        }

        private void AddNewStudent()
        {
            Student student = new Student();
            AddStudentView addStudentDialog = new AddStudentView(student);
            bool? res = addStudentDialog.ShowDialog();
            if (res == true)
            {
                students.Add(student);      // добавляем нового студента без обновления свойства.
                model.AddStudent(student);
            }
        }

        private void EditStudent(Student student)
        {
            AddStudentView addStudentDialog = new AddStudentView(student, true);
            bool? res = addStudentDialog.ShowDialog();
            if (res == true)
            {
                student.NotifyAll();    // обновляем все свойства
                model.SaveChanges();
            }
            else
            {
                model.RollbackStudent(student);     // откат изменений
            }
        }

        private void RemoveStudent(Student student)
        {
            MessageBoxResult res = MessageBox.Show("Delete this student?", "Attention", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (res == MessageBoxResult.Yes)
            {
                model.DeleteStudent(student);
                students.Remove(student);       // из каждой коллекции удаляем отдельно
            }
        }

        private void ShowStudentInfo(Student myStudent)
        {
            StudentInfoView infoDialog = new StudentInfoView(myStudent);
            infoDialog.ShowDialog();
        }


    }
}
